<?php

    require 'functions.php';
    
    if ($_GET['pass']!=UPDATE_PASSWORD)
    {
        die('no access');
    }
    
    $sp = 100;

    $db = DBControl::getInstance();
    //wszystkie produkty, ktore maja wagi i byly dla nich kliknięcia albo love/hate
    $products = $db ->PDOquery("SELECT * FROM itemslikesvectoriginal WHERE item_id IN ((SELECT item_id FROM clicks) UNION DISTINCT (SELECT item_id FROM userschoice));", array());
    //dla kazdego produktu
    foreach ($products as $item)
    {
        //oblicz srednie wagi userow, dla ktorych zapisane sa klikniecia w bazie
        //pobierz wszystkie kliki oraz wszystkie love/hate w bazie i info o userach dla nich, jesli maja podane wagi
        $clicks = $db ->PDOquery("SELECT clicks.item_id as id, weights 
            FROM clicks 
            JOIN users ON clicks.user_id = users.id 
            WHERE item_id = ? AND weights IS NOT NULL
            UNION
            SELECT userschoice.item_id as id, weights 
            FROM userschoice 
            JOIN users ON userschoice.users_id = users.id 
            WHERE item_id = ? AND weights IS NOT NULL AND positive = true;", array($item['id'],$item['id']));
        if (!empty($clicks))
        {
            //zamien wagi na arraye i oblicz srednia
            $clicksWeightsMean = array_fill(0, VECTOR_LENGTH, 0);
            foreach ($clicks as $click)
            {
                $weights = weightsStringToArray($click['weights']);
                for($i = 0; $i < count($weights); $i++)
                {
                    $clicksWeightsMean[$i] += $weights[$i];
                }
            }
            $sum = array_sum($clicksWeightsMean);
            foreach ($clicksWeightsMean as &$weight)
            {
                $weight = $weight/$sum;
            }
            //ustaw nowe wagi jako srednia wazona
            //pobierz wczesniejsze wagi
            $previousWeights = $db ->PDOquery("SELECT * FROM itemslikesvectoriginal WHERE item_id = ?", array($item['id']));
            $previousWeights = weightsStringToArray($previousWeights[0]['weights']);
            echo '$previousWeights:';
            array_display($previousWeights);
            $oldCoeff = $sp/($sp+count($clicks));
            $newCoeff = count($clicks)/($sp+count($clicks));
            
            echo '$clicksWeightsMean:';
            array_display($clicksWeightsMean);
            
            $newWeights = array_fill(0, VECTOR_LENGTH, 0);
            
            for($i = 0; $i < VECTOR_LENGTH; $i++)
            {
                $newWeights[$i] = $oldCoeff * $previousWeights[$i] + $newCoeff * $clicksWeightsMean[$i];
            }
            echo '$newWeights:';
            array_display($newWeights);
            echo '---<br />';
            
            //dodaj nowe wagi do bazy  
            $db -> PDOquery("UPDATE itemslikesvect SET weights = ? WHERE item_id = ?;", array(weightsArrayToString($newWeights), $item['id']));
        }
    }

?>
